<?php

/***
 * 描述：订单模型
 * @category		Lib
 * @package			Model			
 * @subpackage	
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-7-30
 * @version			$Id: OrderModel.class.php 52 2013-11-19 07:34:18Z whtaok@gmail.com $
 */
class OrderModel {
	public static $ORDER_STATUS_WC = 0;
	public static $ORDER_STATUS_XJ = 1;
	public static $ORDER_STATUS_PH = 2;
	public static $ORDER_STATUS_PS = 3;
	public static $ORDER_STATUS_TH = 4;

	public static $CUST_ORDER_STATUS_ZC = 0;
	public static $CUST_ORDER_STATUS_THPART = 1;
	public static $CUST_ORDER_STATUS_THALL = 2;
	/**
	 * 描述：查询指定商铺，指定状态的订单
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		int		$order_status	订单状态
	 * @param 		array	$search			其他搜素条件
	 * @param 		string 	$ordlist		排序
	 * @since		2013-4-21
	 */
	public static function findListPageByOrderStatus($shop_id=0,$order_status=1,$search=array(),$ordlist='id desc'){
		$mod = new CommonModel('OrderLib');
		$modWhere = array(
			'shop_id' => $shop_id,
			'order_status' => $order_status,
			'is_show' => 1,
		);
		if($search['order_code']){
			$modWhere['order_code'] = $search['order_code'];	//订单号
		}
		if($search['cust_mobile']){
			$modWhere['cust_mobile'] = $search['cust_mobile'];	//客户手机
		}
		$totalRows = $mod->where($modWhere)->count();
		list($list,$data) = $mod->where($modWhere)->order($ordlist)->listPage($totalRows);
		return array($list,$data);	
	}
	
	/**
	 * 描述：查询指定商铺，已完成的订单信息
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		array	$search			搜索条件
	 * @param 		string 	$ordlist		排序
	 * @since		2013-4-21
	 */
	public static function findListPageHistory($shop_id=0,$search,$ordlist='id desc'){
		$mod = new CommonModel('OrderLib');
		$modWhere = array(
				'shop_id' => $shop_id,
		);
		if($search['order_status']!=null || $search['order_status']!=''){
			$modWhere['order_status'] = $search['order_status'];
		}
		if($search['cust_order_status']){
			$modWhere['cust_order_status'] = $search['cust_order_status'];	//客户订单状态 1：正常单 2：部分退货单 3：全部退货单（订单取消）
		}
		if($search['order_code']){
			$modWhere['order_code'] = $search['order_code'];	//订单号
		}
		if($search['cust_mobile']){
			$modWhere['cust_mobile'] = $search['cust_mobile'];	//客户手机
		}
		//查询指定时间区域的订单列表
		if($search['cust_order_time_start'] && $search['cust_order_time_end']){
			$modWhere['cust_order_time'] = array('between',array(strtotime($search['cust_order_time_start']),strtotime($search['cust_order_time_end'])+86400));
		}else if($search['cust_order_time_start']){
			$modWhere['cust_order_time'] = array('egt',strtotime($search['cust_order_time_start']));
		}else if($search['cust_order_time_end']){
			$modWhere['cust_order_time'] = array('elt',strtotime($search['cust_order_time_end']));
		}
		$totalRows = $mod->where($modWhere)->count();
		list($list,$data) = $mod->where($modWhere)->order($ordlist)->listPage($totalRows);
		return array($list,$data);
	}
	
	/**
	 * 描述：查询订单详细信息
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		string 	$order_code		订单号
	 * @since		2013-4-21
	 */
	public static function findDetail($shop_id,$order_code){
		$modWhere['order_code'] = $order_code;
		$modWhere['shop_id'] = $shop_id;
		$mod = new CommonModel('OrderLib');
		$order = $mod->where($modWhere)->find();
		if($order){
			$modDetailWhere['order_code'] = $order_code;
			$mod = new CommonModel('OrderDetail');
			$items = $mod->where($modDetailWhere)->select();
			return array($order,$items);
		}else{
			return null;
		}
	}
	

	/**
	 * 描述：根据订单状态，处理订单
	 * 0：完单 1：新订单  2：配货中  3：配送中   4：退货中
	 * 1.处理中的订单
	 * 		（1）如果客户订单状态正常
	 * 			（1.1）可更改状态-》配货中，配送中，退货中
	 * 			（1.2）可更改客户状态-》部分退货，全部退货
	 * 		（2）如果客户订单状态部分退货
	 * 			（2.1）可更改状态-》配货中，配送中
	 * 			（2.2）可更改客户状态-》正常，全部退货
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		string 	$order_code		订单号
	 * @since		2013-4-21
	 */
	public static function checkOrderStep($order_status,$cust_order_status){
		$step = array(
			'step1' => array(
				'name'=>'新订单',
				'step_active'=>'',
				'arrow_active'=>'',
			),
			'step2' => array(
					'name'=>'商品配货',
					'step_active'=>'',
					'arrow_active'=>'',
			),
			'step3' => array(
					'name'=>'订单配送',
					'step_active'=>'',
					'arrow_active'=>'',
			),
			'step4' => array(
					'name'=>'客户支付',
					'step_active'=>'',
					'arrow_active'=>'',
			),
			'step0' => array(
					'name'=>'结单',
					'step_active'=>'',
					'arrow_active'=>'',
			),
		);
		switch($order_status){
			case 1://新订单
				$step['step1']['step_active'] = 'step_active';
				$step['step1']['arrow_active'] = 'arrow_active';
				break;
			case 2://配货中
				$step['step1']['step_active'] = 'step_active';
				$step['step2']['name'] = '配货中';
				$step['step2']['step_active'] = 'step_active';
				$step['step2']['arrow_active'] = 'arrow_active';
				break;
			case 3://配送中
				$step['step1']['step_active'] = 'step_active';
				$step['step2']['name'] = '配货完毕';
				$step['step2']['step_active'] = 'step_active';
				$step['step3']['name'] = '配送中';
				$step['step3']['step_active'] = 'step_active';
				$step['step3']['arrow_active'] = 'arrow_active';
				break;
			case 4://支付中
				$step['step1']['step_active'] = 'step_active';
				$step['step2']['name'] = '配货完毕';
				$step['step2']['step_active'] = 'step_active';
				$step['step3']['name'] = '配送完毕';
				$step['step3']['step_active'] = 'step_active';
				$step['step4']['name'] = '退货中';
				$step['step4']['step_active'] = 'step_active';
				$step['step4']['arrow_active'] = 'arrow_active';
				break;
			case 0://结单
				$step['step1']['step_active'] = 'step_active';
				$step['step2']['name'] = '配货完毕';
				$step['step2']['step_active'] = 'step_active';
				$step['step3']['name'] = '配送完毕';
				$step['step3']['step_active'] = 'step_active';
				if($cust_order_status == 0){
					$step['step4']['name'] = '支付完毕';
				}else if($cust_order_status == 1){
					$step['step4']['name'] = '部分退货';
				}else if($cust_order_status == 2){
					$step['step4']['name'] = '全部退货';
				}else if($cust_order_status == 3){
					$step['step4']['name'] = '订单取消';
				}
				$step['step4']['step_active'] = 'step_active';
				$step['step4']['name'] = '已结单';
				$step['step0']['step_active'] = 'step_active';
				break;
		}
		return $step;
	}
	/**
	 * 描述：更新指定商铺，指定订单号的订单数据
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		string	$order_code		订单号
	 * @param 		array	$orderData		更新数据
	 * @since		2013-4-21
	 */	
	public static function updOrder($shop_id,$order_code,$orderData){
		$modWhere['order_code'] = $order_code;
		$modWhere['shop_id'] = $shop_id;
		$mod = new CommonModel('OrderLib');
		if($mod->where($modWhere)->save($orderData)){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 描述：检查退货状态下的商品列表是否处理过
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		string	$order_code		订单号
	 * @param 		array	$search			其他搜素条件
	 * @param 		string 	$ordlist		排序
	 * @since		2013-4-21
	 */	
	public static function checkCancelOrder($order_code){
		$modDetailWhere['order_code'] = $order_code;
		$mod = new CommonModel('OrderDetail');
		$items = $mod->where($modDetailWhere)->select();
		$total_amt = 0;
		$cancel_amt = 0;
		$cancel_flag = false;
		foreach($items as $key=>$value){
			$total_amt += $value['prod_price_real']*$value['prod_num'];
			$cancel_amt += $value['prod_price_real']*$value['prod_cancel_num'];
		}
		if($cancel_amt>0){
			$cancel_flag = true;
		}
		return array($cancel_flag,$total_amt,$cancel_amt);
	}
	
	/**
	 * 描述：订单全部退货，订单取消
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string	$order_code		订单号
	 * @since		2013-4-21
	 */
	public static function cancelOrderItemAll($order_code){
		$modDetailWhere['order_code'] = $order_code;
		$mod = new CommonModel('OrderDetail');
		$flag = $mod->query("update order_detail set prod_cancel_num = prod_num where order_code = '".$order_code."'");
		if($flag!==false){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 描述：单品退货
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$order_code		订单号
	 * @param 		string 	$id				退货ID
	 * @param 		int		$num			退货数量
	 * @since		2013-4-21
	 */
	public static function cancelOrderItem($order_code,$id,$num){
		$modDetailWhere['order_code'] = $order_code;
		$modDetailWhere['id'] = $id;
		$mod = new CommonModel('OrderDetail');
		$data['prod_cancel_num'] = $num;
		$flag = $mod->where($modDetailWhere)->save($data);
		if($flag!==false){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 描述：查询订单商品
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		array 	$search			查询条件
	 * @param 		array	$ordlis			排序
	 * @since		2013-4-21
	 */
	public static function findProdList($shop_id,$search,$ordlist='id desc'){
		//按订单号查询
		if($search['order_code']){
			$modWhere['order_code']=$search['order_code'];
		}
		//按商品名称
		if($search['prod_name']){
			$modWhere['prod_name']=array('like','%'.$search['prod_name'].'%');
		}
		//订单状态0：结单 
		if($search['order_status']>=0){
			$modWhere['order_status']=$search['order_status'];
		}
		//客户订单订单状态 0 正常  1部分退货  2全部退货  3订单取消
		if($search['cust_order_status']>=0){
			$modWhere['cust_order_status']=$search['cust_order_status'];
		}

		$mod = new OrderProductViewModel();
		$rtnData = $mod->findProdListPage($shop_id, $modWhere, $ordlist);
		return $rtnData;
	}
	
	/**
	 * 描述：查询商品详细信息
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$order_code		订单号
	 * @param 		string 	$id				商品表ID
	 * @since		2013-4-21
	 */
	public static function findProdDetail($order_code,$id){
		$modWhere['order_code'] = $order_code;
		$modWhere['id'] = $id;
		$mod = new CommonModel('OrderDetail');
		$prod = $mod->where($modWhere)->find();
		return $prod;
	}
	
	/**
	 * 描述：查询退货商品
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		array 	$search			查询条件
	 * @param 		array	$ordlis			排序
	 * @since		2013-4-21
	 */
	public static function findProdCancelList($shop_id,$search,$ordlist='id desc'){

		$modWhere['order_status']=0;	//结单
		$modWhere['cust_order_status']= array('in',array(1,2,3)); //部分退货，全部退货 ，取消订单
		$modWhere['prod_cancel_num'] =  array('gt',0);
		
		if($search['order_code']){
			$modWhere['order_code']=$search['order_code'];//按订单号查询
		}	
		if($search['prod_name']){
			$modWhere['prod_name']=array('like','%'.$search['prod_name'].'%');//按商品名称
		}
		if($search['prod_cancel_stock_status']>=0){
			$modWhere['prod_cancel_stock_status']=$search['prod_cancel_stock_status'];//入库状态查询
		}
		$mod = new OrderProductViewModel();
		$rtnData = $mod->findProdListPage($shop_id, $modWhere, $ordlist);
		return $rtnData;
	}
		
}

?>